/*
 * Project it.battlehorse.rcp.tools.actors.sampledb
 * Created on Dec 2, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.actors.sampledb;

import it.battlehorse.rcp.tools.actors.IActor;
import it.battlehorse.rcp.tools.actors.IActorRunnable;
import it.battlehorse.rcp.tools.actors.sampledb.view.SampleDBView;
import it.battlehorse.rcp.tools.commons.Severity;
import it.battlehorse.rcp.tools.commons.actors.BasicValidationStatus;
import it.battlehorse.rcp.tools.commons.actors.IValidationStatus;

import org.eclipse.ui.IViewPart;

/**
 * The actor which validates the query submitted by the user and executes it.
 * 
 * @author battlehorse
 * @since Dec 2, 2005
 */
public class SampleDbActor implements IActor {
	
	public static final String ID_ACTOR = "it.battlehorse.rcp.tools.actors.sampledb.SampleDbActor";

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.actors.IActor#validate(org.eclipse.ui.IViewPart)
	 */
	public IValidationStatus validate(IViewPart view) {
		SampleDBView v = (SampleDBView) view;
		
		BasicValidationStatus status ;
		if (!v.getQuery().trim().toLowerCase().startsWith("select")) {
			status = new BasicValidationStatus("You must enter a SELECT query",false,null,Severity.ERROR);
		}
		else if (v.getSelectedDatasource() == null)
			status = new BasicValidationStatus("You must select a Datasource",false,null,Severity.ERROR);
		else 
			status = new BasicValidationStatus("Parameters are correct",true,null,Severity.INFO);
		
		return status;
		
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.rcp.tools.actors.IActor#getRunnable(org.eclipse.ui.IViewPart)
	 */
	public IActorRunnable getRunnable(IViewPart view) {
		SampleDBView v = (SampleDBView) view;
		return new SampleDbJob(v.getSelectedDatasource(),v.getQuery());
	}

}
